const app = getApp();
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		imgArr: { // 图片数组
			type: [Array],
		},
		uploadImgCount: { // 一次上传图片数
			type: String,
			value: '9'
		},
		imgCount: { // 可上传图片总数
			type: String,
			value: '9'
		},
		imgSize: { // 图片大小 MB （默认5MB）
			type: Number,
			value: 5
		},
		closeTip: { // 是否关闭 下方提示文字
			type: Boolean,
			value: false
		},
		showDelete:{ // 是否 显示删除
			type: Boolean,
			value: true
		},
		url: { // 文件上传路径
			type: String,
			value: ''
		},
		haeder: { // 文件上传请求头
			type: Object,
			value: {}
		},
		formData:{// 文件上传携带参数
			type: Object,
			value: {}
		}
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		imgArray: [],
		canUpCount: '',
	},
	attached: function () {
		this.data.canUpCount = Number(this.data.uploadImgCount);
	},
	/**
	 * 监听
	 */
	observers: {
		'imgArr': function (val) {
			if (val == null) return;
			this.setData({
				imgArray: val
			})
		}
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		// 图片上传
		upload(callback) {
			const _this = this;
			if (_this.data.imgArray instanceof Array && _this.data.imgArray.length) {
				let successNum = 0;
				let results = [];
				for (let item of _this.data.imgArray) {
					// 调用上传
					_this.uploadImg(item, res => {
						// TODO 可自定义
						if (res.code == 0) {
							successNum++;
							results.push(res.url);
						} else {
							results.push(res);
						}
						if (results.length == _this.data.imgArray.length) {
							callback(_this.result(results, successNum));
						}
					});
				}
			} else {
				callback({
					result: 'warning',
					code: 400,
					msg: '请先上传图片！'
				});
			}
		},
		upPhoto() {
			let that = this;
			if (Number(that.properties.imgCount - that.data.imgArray.length) < Number(that.properties.uploadImgCount)) {
				that.data.canUpCount = Number(that.properties.imgCount - that.data.imgArray.length);
			}
			wx.showActionSheet({
				itemList: ['拍照上传', '从相册中选择'],
				success: function (res) {
					if (res.tapIndex == 0) {
						wx.chooseImage({
							count: Number(that.data.canUpCount),
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['camera'], //从相册选择
							success: function (res) {
								if (res) {
									if (res.tempFiles instanceof Array && res.tempFiles) {
										for (let item of res.tempFiles) {
											if (item.size > (that.data.imgSize * 1024 * 1024)) {
												wx.showToast({
													title: `图片不能大于${that.imgSize}M`,
													icon: 'none'
												})
												return false;
											}
										}
									}
									that.data.imgArray = [...that.data.imgArray, ...res.tempFilePaths];
									that.setData({
										imgArray: that.data.imgArray
									})
								}
							}
						});
					} else if (res.tapIndex == 1) {
						wx.chooseImage({
							count: Number(that.data.canUpCount),
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album'], //从相册选择
							success: function (res) {
								if (res) {
									if (res.tempFiles instanceof Array && res.tempFiles) {
										for (let item of res.tempFiles) {
											if (item.size > (that.data.imgSize * 1024 * 1024)) {
												wx.showToast({
													title: `图片不能大于${that.data.imgSize}M`,
													icon: 'none'
												})
												return false;
											}
										}
									}
									that.data.imgArray = [...that.data.imgArray, ...res.tempFilePaths];
									that.setData({
										imgArray: that.data.imgArray
									})
								}
							}
						});
					}
				},
				fail: function (res) {
					console.log(res.errMsg);
				}
			});
		},
		preview(e) {
			// 预览图片
			wx.previewImage({
				urls: [e.currentTarget.dataset.value]
			});
		},
		delImg(e) {
			const _this = this;
			wx.showModal({
				title: '提示',
				content: '是否删除这张照片？',
				success: function (res) {
					if (res.confirm) {
						_this.data.imgArray.splice(e.currentTarget.dataset.i, 1);
						_this.setData({
							imgArray: _this.data.imgArray
						})
					} else if (res.cancel) {}
				}
			});
		},
		result(responseList, successNum) {
			let result = {
				code: 0,
				success: true,
				successNum: successNum,
				result: responseList
			}
			return result;
		},
		uploadImg(item, callback) {
			const _this = this;
			if (!_this.data.url) {
				console.error("[图片上传]上传图片-路径不能为空！");
			}
			wx.uploadFile({
				url: _this.data.url, //自行修改各自的对应的接口
				filePath: item,
				name: 'files',
				haeder: _this.data.haeder,
				formData:_this.data.formData,
				success: (uploadFileRes) => {
					if (uploadFileRes) {
						if (!uploadFileRes.data) {
							console.error("[图片上传]上传图片-响应为空!");
							return
						}
						callback(uploadFileRes.data);
					}
				},
				fail: function (res) {
					console.error(res.errMsg);
				}
			});
		}
	}
})